package com.jinke.api.common.util;

import com.jinke.api.modules.app.response.UserOrderSummaryVO;
import com.jinke.api.modules.third.ezeeship.model.request.EstimateRateRequest;
import com.jinke.api.modules.ums.model.Channel;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;

import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Slf4j
public class CommonUtil {

    /**
     * 是否是tc的
     * @param userId
     * @return
     */
    public static boolean checkTC(Integer userId) {
        List<Integer> integerList = getTachengUsers();
        return  integerList.contains(userId);


    }

    /**
     * 获取塔城用户
     * @return
     */
    @NotNull
    public static List<Integer> getTachengUsers() {
        List<Integer> integerList = new ArrayList<>();
        integerList.add(283);
        integerList.add(284);
        integerList.add(288);
        integerList.add(292);
        integerList.add(293);
        integerList.add(295);
        integerList.add(297);
        integerList.add(300);
        integerList.add(302);
        integerList.add(303);
        integerList.add(304);
        integerList.add(305);
        integerList.add(306);
        integerList.add(307);
        integerList.add(308);
        integerList.add(309);
        integerList.add(310);
        integerList.add(311);
        integerList.add(312);
        integerList.add(314);
        integerList.add(320);
        integerList.add(321);
        integerList.add(324);
        integerList.add(329);
        integerList.add(332);
        integerList.add(276);
        //TC-47TZT
        integerList.add(346);
        return integerList;
    }


    /**
     * 随机生成订单号
     *
     * @return
     */
    public  static String getOrderNo() {
        UUID uuid4 = UUID.randomUUID();
        String suffix = uuid4.toString().replaceAll("-", "");
        String prefix = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        return prefix + suffix.substring(suffix.length() - 6);
    }

    /**
     * 随机生成订单号
     *
     * @return
     * @param request
     */
    public  static void handle(EstimateRateRequest request) {
        try {

            Integer batteryType = request.getExtra().getBatteryType();
            if (batteryType!=null&&batteryType == 0) {
                request.getExtra().setBatteryType(null);
            }
        } catch (Exception x) {
            log.error(x.getMessage() + "转电池出错");
        }
        try {
            CommonUtil.handleChar(request);
        } catch (Exception x) {
            log.error(x.getMessage() + "转字符出错");
        }


    }


    /**
     * 处理请求中的特殊字符
     * @param request
     */
    private static void handleChar(EstimateRateRequest request) {
        String personName = request.getFrom().getPersonName();
        String addressLine1 = request.getFrom().getAddressLine1();
        if (personName!=null&&personName.contains("-")){
            String replace = personName.replace("-", " ");
            request.getFrom().setPersonName(replace);
        }
        if (addressLine1!=null&&addressLine1.contains("-")){
            String adressNew = addressLine1.replace(";", " ");
            request.getFrom().setAddressLine1(adressNew);
        }

    }

    public static void main(String[] args) {
        String unicodeStr = "{\"result\":[{\"code\":400,\"orderid\":\"test-YANCHI-REAL\",\"msg\":\"\\u8bf7\\u68c0\\u67e5\\u5907\\u6ce8\\uff0c\\u4e0d\\u80fd\\u5305\\u542b\\u4e2d\\u6587\"}]}";
        String convertedStr = convertToUtf8(unicodeStr);
        System.out.println(convertedStr);
    }

    public static  String createMsgHtml(List<UserOrderSummaryVO> userOrderSummaryList, String realName, String txtDownloadURL) {
        StringBuilder htmlBuilder = new StringBuilder();
//        htmlBuilder.append("<body>");

        htmlBuilder.append("<h3>用户 ").append(realName).append(" 的订单统计</h3>");
        htmlBuilder.append("<h4>下载 ").append(txtDownloadURL + "</h4>");

        htmlBuilder.append("<table border='1'>");
        htmlBuilder.append("<tr><th>用户姓名</th><th>订单日期</th><th>无轨迹订单数</th><th>总订单数</th><th>无轨迹率</th></tr>");

        for (UserOrderSummaryVO vo : userOrderSummaryList) {

            htmlBuilder.append("<tr>");
            htmlBuilder.append("<td>").append(realName).append("</td>");
            htmlBuilder.append("<td>").append(vo.getOrderDate()).append("</td>");
            htmlBuilder.append("<td>").append(vo.getNoInfoCount()).append("</td>");
            htmlBuilder.append("<td>").append(vo.getTotalOrderCount()).append("</td>");
            htmlBuilder.append("<td>").append(vo.getNoInfoPercentage()).append("%</td>");
            htmlBuilder.append("</tr>");
        }
        htmlBuilder.append("</table>");
//        htmlBuilder.append("</body>");


        return htmlBuilder.toString();
    }



    public static String convertToUtf8(String unicodeStr) {
        try {
            // 使用 Java 的内置解码器进行转换
            return java.util.UUID.nameUUIDFromBytes(unicodeStr.getBytes(StandardCharsets.ISO_8859_1))
                    .toString().replaceAll("-", "");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 通过渠道推测大概的运输时间
     * @param channel
     * @return
     */
    public static String getDeliveryTime(Channel channel) {
        switch (channel.getServiceCode()) {
            case "延迟单":
                return "arrive in 3-5 days";
            case "USPS PRIORITY MAIL":
                return "arrive in 2-3 days";
            case "ups 2nd day":
                return "arrive in 2 days";
            case "ups next day":
                return "arrive in 1 day";
            case "USPS First Class":
                return "arrive in 3-5 days";
            case "USPS Media Mail":
                return "arrive in 2-8 days";
            case "USPS Parcel Select Ground":
                return "arrive in 2-8 days";
            case "USPS GROUND ADVANTAGE":
                return "arrive in 2-5 days";
            case "USPS PRIORITY MAIL EXPRESS":
                return "arrive in 1-2 days";
            case "ups 3 day":
                return "arrive in 3 days";
            case "UPS® Ground":
                return "arrive in 1-5 days";
            case "UPS SUREPOST":
                return "arrive in 2-7 days";
            case "UPS Worldwide Express Saver":
                return "arrive in 2-5 days";
            case "UPS Worldwide Express®":
                return "arrive in 1-3 days";
            case "UPS Worldwide Expedited®":
                return "arrive in 2-6 days";
            case "FedEx Home Delivery®":
                return "arrive in 1-5 days";
            case "FedEx Ground®":
                return "arrive in 1-7 days";
            case "Smart Post":
                return "arrive in 2-7 days";
            case "UPS Worldwide Express":
                return "arrive in 1-3 days";
            case "UPS Worldwide Expedited":
                return "arrive in 2-6 days";
            case "USPS First Class Package International Service":
                return "arrive in 7-21 days";
            case "USPS First Class Mail International":
                return "arrive in 7-21 days";
            case "USPS FC Mail Letter":
                return "arrive in 7-21 days";
            case "USPS FC Mail Flat":
                return "arrive in 7-21 days";
            case "UPS Ground Saver":
                return "arrive in 3-7 days";
            default:
                return "";
        }
    }
}
